package entity.connector;

import entity.bean.House;
import entity.bean.Show;
import java.sql.*;

/**
 *
 * @author 52571783
 */
public class HouseConnector {
    private static int noOfHouses;
    private static House[] houses;
    
    static {
        try {
            Connection con = ConnectionProvider.getCon();
            Statement stmt =
                    con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
                    ResultSet.CONCUR_READ_ONLY);
            ResultSet rs = stmt.executeQuery("SELECT * FROM house ORDER BY house_id");
            // find the number of houses
            rs.last();
            noOfHouses = rs.getRow();
            houses = new House[noOfHouses];
            // store data from result set to static data members
            rs.beforeFirst();
            for (int i = 0; rs.next() != false; i++) {
                houses[i] = new House(i+1);
                houses[i].setRows(rs.getInt("rows"));
                houses[i].setCols(rs.getInt("cols"));
            }
            // close resources after use
            rs.close();
            stmt.close();
        } catch (Exception e) {
        }
    }
    
    public static int getNoOfHouses(){
        return noOfHouses;
    }
    
    public static House getHouse(int houseID){
        return houses[houseID-1];
    }
    
    public static House getHouse(Show show){
        return houses[show.getHouseID()-1];
    }
    
/*  public static int noOfHouses;
    public static int[] noOfRows;
    public static int[] noOfCols;

    static {
        try {
            Connection con = ConnectionProvider.getCon();
            Statement stmt =
                    con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
                    ResultSet.CONCUR_READ_ONLY);
            ResultSet rs = stmt.executeQuery("SELECT * FROM [house] ORDER BY [house_no] ASC");
            // find the number of houses
            while (rs != null && rs.next() != false) {
                noOfHouses = rs.getRow();
            }
            noOfRows = new int[noOfHouses];
            noOfCols = new int[noOfHouses];
            // store data from result set to static data members
            rs.beforeFirst();
            for (int i = 0; rs.next() != false; i++) {
                noOfRows[i] = rs.getInt("rows");
                noOfCols[i] = rs.getInt("columns");
            }
            // close resources after use
            if (rs != null) {
                rs.close();
            }
            if (stmt != null) {
                stmt.close();
            }
        } catch (Exception e) {
        }
    }

    public static void updateRowCol(House house) throws SQLException {
        int houseNo = house.getHouseNo();
        if (houseNo <= noOfHouses) {
            house.setRow(noOfRows[houseNo - 1]);
            house.setCol(noOfCols[houseNo - 1]);
        }
    }*/
}